package com.ck.ips.controller;

import com.ck.core.cache.redis.RedisCacheDao;
import com.ck.core.constant.CacheConstant;
import com.ck.core.constant.ImageCodeType;
import com.ck.core.util.ImageCodeUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @Description: 校验类controller
* @Author: hf
* @Date:    2019/4/29 9:43
* @Version: 1.0
*/
@RestController
public class ValidateController {
    private static Logger logger = LoggerFactory.getLogger(ValidateController.class);
    @Autowired
    private RedisCacheDao redisCacheDao;

    /**
     * 获取图片验证码
     */
    @PostMapping("/image/code")
    public void getCode(HttpServletRequest request, HttpServletResponse response,Integer width,Integer height, Integer codeCount, Integer lineCount,String key) throws Exception{
        if ( StringUtils.isNotBlank(key) ) {
            response.setContentType("image/jpeg");
            //禁止图像缓存
            response.setHeader("Pragma","no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            ImageCodeUtil imageUtil = new ImageCodeUtil(width, height, codeCount,lineCount);
            imageUtil.write(response.getOutputStream());

            // 记录此次验证码
            redisCacheDao.putCache(CacheConstant.CACHE_NAME_CK_IMAGE_CODE_PREFIX + ImageCodeType.LOGIN.name(), key,imageUtil.getCode());

        }
    }




}
